<?php
namespace page;

class Fpage{
    private $page;//当前页
    private $pagenum;//总页数
    private $newLimit; //每页展示几条数据
    private $newCount; //数据总数
    private $url;
    public function __construct($page,$newCount,$newLimit,$url=''){
        $this->page=$page;
        $this -> newCount = $newCount;
        $this->newLimit = $newLimit;
        if($newCount == 0){
            $this -> pagenum = 0;
        }else{
            $this -> pagenum = ceil($this->newCount/$this ->newLimit);
        }
        $this -> url = $url;
    }

    //首页
    private function first(){
        if($this->page==1){
            //@$html.='<span>首页</span>';
            @$html .='';
        }else{
//            @$html.='<a href="?'.$this -> url.'page=1">首页</a>';
            @$html.='<li class="first"><a href="?'.$this -> url.'page=1">首页</a></li>';
//            @$html.='<a href="?'.$this -> url.'page=1">first page</a>';
        }
        return $html;
    }
    //上一页
    private function prev(){
        if($this->page==1){
            //@$html.='<span>上一页</span>';
            @$html .='';
        }else{
//            @$html.='<a class="prev" href="?'.$this -> url.'page='.($this->page-1).'"><</a>';
//            @$html.='<a class="prev" href="?'.$this -> url.'page='.($this->page-1).'">上一页</a>';
//            @$html.='<li><a class="prev" href="?'.$this -> url.'page='.($this->page-1).'"><</a></li>';
            @$html.='<li class="prev"><a href="?'.$this -> url.'page='.($this->page-1).'"><<</a></li>';
        }
        return $html;
    }
    //下一页
    private function next(){
        if($this->page >= $this->pagenum){
            //@$html.='<span>下一页</span>';
            @$html .='';
        }else{
//            @$html.='<a class="next" href="?'.$this -> url.'page='.($this->page+1).'">></a>';
//            @$html.='<a class="next" href="?'.$this -> url.'page='.($this->page+1).'">下一页</a>';
//            @$html.='<li><a class="next" href="?'.$this -> url.'page='.($this->page+1).'">></a></li>';
            @$html.='<li class="next"><a href="?'.$this -> url.'page='.($this->page+1).'">>></a></li>';
        }
        return $html;
    }

    //循环展示页数
    private function pageNum(){
        if($this->pagenum > 1){
            for ($i=1;$i<=$this->pagenum;$i++){
                if($i==$this ->page){
//                    @$html .= '<a class="active" href="?'.$this -> url.'page='.$i.'" title="">'.$i.'</a>';
                    @$html .= '<li class="active"><a href="?'.$this -> url.'page='.$i.'" title="">'.$i.'</a></li>';
                }else{
//                    @$html .= '<a href="?'.$this -> url.'page='.$i.'" title="">'.$i.'</a>';
                    @$html .= '<li><a href="?'.$this -> url.'page='.$i.'" title="">'.$i.'</a></li>';
                }
            }
            return $html;
        }else{
            return '';
        }
    }

    //尾页
    private function last(){
        if($this->page==$this->pagenum || $this -> newCount == 0){
            //@$html.='<span>'.$this->pagenum.'</span>';

        }else{
//            @$html.='<li><a href="?'.$this -> url.'page='.($this->pagenum).'">last page</a></li>';
//            @$html.='<a href="?'.$this -> url.'page='.($this->pagenum).'">尾页</a>';
            @$html.='<li class="last"><a href="?'.$this -> url.'page='.($this->pagenum).'">尾页</a></li>';
        }
        return $html;
    }

    private function jump(){
        if($this -> pagenum < 2){
            @$html .= '';
        }else{
            @$html .= '<li><form id="pageform" action="" method="get" onsubmit="return jumpPage()"><span>到第</span><input type="text" name="page"><span>页</span><span class="jump" onclick="jumpPage()">跳转</span><span id="pageCountNum">共'.$this->pagenum.'页</span></form></li>';
        }
        return $html;
    }


    public function pagelist(){
//        return array('prev'=>$this->prev(),'pageNum'=>$this->pagenum(),'next'=>$this->next(),'jump'=>$this->jump());
//        return array('first'=>$this->first(),'pageNum'=>$this->pagenum(),'last'=>$this->last());
        return array('first'=>$this->first(),'prev'=>$this->prev(),'pageNum'=>$this->pagenum(),'next'=>$this->next(),'last'=>$this->last());
    }
}